package com.cungmua.app.task;

import android.content.Context;

import com.cungmua.app.handler.BaseAuthenticatedRequestHandler;
import com.cungmua.app.handler.ChangePassHandler;
import com.cungmua.app.model.request.ChangePassRequest;
import com.cungmua.app.model.response.ChangePassResponse;
import com.cungmua.app.net.CungMuaAPI;

import retrofit.RetrofitError;

/**
 * Created by kelvin.nguyen on 7/2/14.
 */
public class ChangePassTask extends AuthenticatedTask<ChangePassRequest, Void, ChangePassResponse> {
    private Context context;
    private ChangePassHandler handler;

    public ChangePassTask(Context context, ChangePassHandler handler) {
        this.context = context;
        this.handler = handler;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected ChangePassResponse doInBackground() throws RetrofitError {
        ChangePassRequest request = params[0];
        return CungMuaAPI.getService().changePassword(request.toFieldMap());
    }

    @Override
    protected BaseAuthenticatedRequestHandler getHandler() {
        return handler;
    }

    @Override
    protected void onSuccess(ChangePassResponse changePassResponse) {
        changePassResponse.setNewPass(params[0].getNewPass());
        handler.onPasswordChange(changePassResponse);
    }
}
